<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhuyajie <xcoolcc@gmail.com>
// +----------------------------------------------------------------------

namespace Admin\Model;
use Common\Model\CommonModel;


/**
 * 权限规则（后台菜单）模型
 * @author 朱亚杰 <zhuyajie@topthink.net>
 */
class AuthRuleModel extends CommonModel{
    
    const RULE_URL = 1;
    const RULE_MAIN = 2;


       //自动验证
    protected $_validate = array(
        //array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
        array('title', 'require', '菜单名称不能为空！', 1, 'regex', CommonModel:: MODEL_BOTH ),
        array('name', 'require', '应用不能为空！', 1, 'regex', CommonModel:: MODEL_BOTH ),
       array('name', '', '同样的记录已经存在！', 0 ,'unique', CommonModel:: MODEL_BOTH   ),
         array('parentid', 'checkParentid', '菜单只支持四级！', 1, 'callback', 1),
    );
    //自动完成
    protected $_auto = array(
    		//array(填充字段,填充内容,填充条件,附加规则)
    );
    
    //验证菜单是否超出三级
    public function checkParentid($parentid) {
    	$find = $this->where(array("id" => $parentid))->getField("parentid");
    	if ($find) {
    		$find2 = $this->where(array("id" => $find))->getField("parentid");
    		if ($find2) {
    			$find3 = $this->where(array("id" => $find2))->getField("parentid");
    			if ($find3) {
    				$find4 = $this->where(array("id" => $find3))->getField("parentid");
    				if ($find4) {
    					return false;
    					
    				}
    			}
    		}
    	}
    	return true;
    }

    

    /**
     * 菜单树状结构集合
     */
    public function s($name) {

		$gourp_num=D("AuthGroupAccess")->where(array("uid"=>UID,"group_id"=>"1"))->count();


		if(UID!=1&&!$gourp_num){



			$Auths = new \Think\Auth ();
			$ru=$Auths->getGroups(UID);



			$map[id]=array("in",$ru);



		}
		$map[module]=$name;
		$map[status]=1;
		$map[display]=1;
		$map[right]=0;



		//	dump($map);

		$menu=$this->where($map)->order(array("listorder" => "DESC"))->select();

//		echo M()->getlastsql();
//
//		dump($menu);

		import("Tree");
		$tree = new \Tree();
		$tree->init($menu);


		$data = $tree->get_tree_array(0);
		//return array_merge($changyong, $data);
		return $data;
	}


	public function data_cache(){
    	
    	$data=$this->where(array("status"=>1))->select();
    	
    	foreach ($data as $key=>$v){
    		
    		$result[$v[module]][]=$v;
    		
    		
    	}
    	
    	foreach ($result as $key=>$v){
    		
    		F("Menu_".$key,$v);
    		
    	}
    	
    	return true;
    }


    
    

    
    public function menu($parentid, $with_self = false){
    	//父节点ID
    	$parentid = (int) $parentid;
    	$result = $this->where(array('parentid' => $parentid))->select();
    	if ($with_self) {
    		$result2[] = $this->where(array('id' => $parentid))->find();
    		$result = array_merge($result2, $result);
    	}
    	return $result;
    }
    
    
}
